Arrays and images

resource used: http://learningtensorflow.com/lesson3/

1: Read an image


In [25]:
import os
import matplotlib.image as mpimg

dir_path = os.getcwd()
filename = dir_path + "\\MarshOrchid.jpg"

image = mpimg.imread(filename)

2. Shape of an image


In [26]:
print(image.shape)


(5528, 3685, 3)

3. View the image


In [30]:
import matplotlib.pyplot as plt

plt.imshow(image)
plt.show()


4. Transpose an image


In [33]:
import tensorflow as tf
import matplotlib.image as mpimg
import matplotlib.pyplot as plt
import os

# First, load the image again
dir_path = os.getcwd()
filename = dir_path + "\\MarshOrchid.jpg"
image = mpimg.imread(filename)

# Create a TensorFlow Variable
x = tf.Variable(image, name='x')

model = tf.global_variables_initializer()

with tf.Session() as session:
    x = tf.transpose(x, perm=[1, 0, 2])
    session.run(model)
    result = session.run(x)


plt.imshow(result)
plt.show()


5. Reverse an image


In [47]:
import numpy as np
import tensorflow as tf
import matplotlib.image as mpimg
import matplotlib.pyplot as plt
import os
# First, load the image again
dir_path = os.getcwd()
filename = dir_path + "\\MarshOrchid.jpg"
image = mpimg.imread(filename)
height, width, depth = image.shape

# Create a TensorFlow Variable
x = tf.Variable(image, name='x')
y = tf.reverse_sequence(x, [width] * (height), seq_axis=1, batch_dim=None)


model = tf.global_variables_initializer()

with tf.Session() as session:
    session.run(model)
    result = session.run(y)

print(result.shape)
plt.imshow(result)
plt.show()


(5528, 3685, 3)

6. Rotate Clockwise


In [6]:
import tensorflow as tf
import matplotlib.image as mpimg
import matplotlib.pyplot as plt
import os

# First, load the image again
dir_path = os.getcwd()
filename = dir_path + "\\MarshOrchid.jpg"
image = mpimg.imread(filename)
height, width, depth = image.shape


# Create a TensorFlow Variable
x = tf.Variable(image, name='x')
y = tf.transpose(x, perm=[1, 0, 2])
z = tf.reverse_sequence(y, [height] * (width), seq_axis=1, batch_dim=None)
 

model = tf.global_variables_initializer()

with tf.Session() as session:
    session.run(model)
    result = session.run(z)


plt.imshow(result)
plt.show()


7. Flip upside down


In [18]:
import numpy as np
import tensorflow as tf
import matplotlib.image as mpimg
import matplotlib.pyplot as plt
import os
# First, load the image again
dir_path = os.getcwd()
filename = dir_path + "\\MarshOrchid.jpg"
image = mpimg.imread(filename)
height, width, depth = image.shape

# Create a TensorFlow Variable
x = tf.Variable(image, name='x')
y = tf.reverse_v2 (x, axis=[0], name= None)


model = tf.global_variables_initializer()

with tf.Session() as session:
    session.run(model)
    result = session.run(y)

print(result.shape)
plt.imshow(result)
plt.show()


(5528, 3685, 3)

8. Mirror image


In [28]:
import numpy as np
import tensorflow as tf
import matplotlib.image as mpimg
import matplotlib.pyplot as plt
import os
# First, load the image again
dir_path = os.getcwd()
filename = dir_path + "\\MarshOrchid.jpg"
image = mpimg.imread(filename)
height, width, depth = image.shape

# Create a TensorFlow Variable
x = tf.Variable(image, name='x')
y = tf.reverse_sequence(x, [round(width/2)] * (height), seq_axis=1, batch_dim=None)


model = tf.global_variables_initializer()

with tf.Session() as session:
    session.run(model)
    result = session.run(y)

plt.imshow(result)
plt.show()



In [30]:
height, width, depth = result.shape
width = round(width/2)
temp = result[:,0:width,:]
temp_reverse = temp[:,::-1,:]

mirror_image = np.concatenate((temp_reverse,temp), axis=1)

plt.imshow(mirror_image)
plt.show()